// import JSEncrypt from "jsencrypt/bin/jsencrypt";
import jsencrypt from '@/js_sdk/jsencrypt-Rsa/jsencrypt/jsencrypt.vue';
import {getLoginKeyApi} from "@/api/login.js";


let requestTask = null

const getLoginKey = () => {
    if (!requestTask) {
        requestTask = new Promise(async (resolve, reject) => {
            try {
                const res = await getLoginKeyApi()
                console.log('getLoginKeyApi',res);
                const key = res.data
                uni.setStorageSync('loginKey', key)
                // window.sessionStorage.setItem('loginKey', key)
                resolve(key)
            } catch (e) {
                reject(e)
            } finally {
                requestTask = null
            }
        })
    }
    return requestTask
}

const encrypt = async (password) => {
    let key = uni.getStorageSync('loginKey') || await getLoginKey()
    // let encryptor = new jsencrypt();
    var pubblicData=jsencrypt.setEncrypt(key,password);
    console.log('key',key);
    console.log('password',password);
    return pubblicData;
    console.log('pubblicData',pubblicData);
    // encryptor.setPublicKey(key);
    // return encryptor.encrypt(password);
}

export default encrypt